<?php
// +----------------------------------------------------------------------
// | 简易CMS
// +----------------------------------------------------------------------
// | Copyright (C) 2010 Hoping Software Studio.
// +----------------------------------------------------------------------
// | Version $Id$
// +----------------------------------------------------------------------
namespace app\admin\controller;

/**
 * @package		简易CMS
 * @subpackage	搜索引擎来访记录管理模块
 */
class spider extends AdminController {

	function __construct() {
		parent::__construct();
	}

	function index() {
		global $_G;
		$model = D('Spider');
		$logmodel = D('SpiderLog');
		$ipmodel = D('SpiderIp');
		if (submitcheck()) {
			$names = dimplode($_G['gp_delete']);
			if ($names) {
				$model->where("name IN ($names)")->delete();
				$ipmodel->where("name IN ($names)")->delete();
				$logmodel->where("name IN ($names)")->delete();
			}
		}
		$order_by = $_G['gp_order_by'] ? $_G['gp_order_by'] : "today_times";
			$order_type = $_G['gp_order_type'] ? $_G['gp_order_type'] : "desc";
			$toggle_order_type = $order_type == "desc" ? "asc" : "desc";
			$$order_by = "order_" . $order_type;
			$lists = $model->select();
			$sql = "SELECT * FROM #__spider";
			$robot_list = array();
			$name_list = array();
			foreach ((array) $lists as $row) {
				$row['link'] = preg_replace("/.*?(((((https?|ftp|gopher|news|telnet|rtsp|mms|callto|bctp|ed2k):\/\/)|(www\.))([^\[\"'\s\)\(\;]+))|([a-z0-9\_\-.]+@[a-z0-9]+\.[a-z0-9\.]{2,}))*/i", "\\1", $row['agent']);
				if (strpos($row['link'], '@') !== false)
					$row['link'] = "mailto:" . $row['link'];
				if ($row['link'] && strpos($row['link'], ":") === false)
					$row['link'] = "http:/" . "/" . $row['link'];
				$row['first_visit'] = dgmdate($row['first_visit'], "Y-m-d");
				$row['last_visit'] = dgmdate($row['last_visit'], "Y-m-d");
				$row['today_times'] = 0;
				$name_list[] = $row['name'];
				$row['name'] = trim($row['name']);
				$robot_list[$row['name']] = $row;
			}
			$today = date("Y-m-d", time());
			if (sizeof($name_list) > 0) {
				$names = implode("','", $name_list);
				$names = "'" . $names . "'";
				$today_robot_list = array();
				$rows = $logmodel->where("`name` IN ($names) AND `date`='$today'")->order('times DESC')->select();
				foreach ((array) $rows as $row) {
					if (isset($robot_list[$row['name']]))
						$robot_list[$row['name']]['today_times'] = $row['times'];
				}
			}

			if (is_array($robot_list) && sizeof($robot_list) > 0) {
				foreach ($robot_list as $key => $value) {
					$order_by_list[$key] = $value[$order_by];
				}
				array_multisort($order_by_list, constant(strtoupper("sort_" . $order_type)), $robot_list);
			}
			if (sizeof($robot_list) > 0) {
				$robot_ip_list = array();
				$rows = $ipmodel->order("`ip`")->select();
				foreach ((array) $rows as $row) {
					$robot_ip_list[$row['name']][] = $row['ip'];
				}
				if (!empty($robot_ip_list)) {
					foreach ($robot_ip_list as $_robot => $_ip_list) {
						if (sizeof($_ip_list) > 5) {
							$ip_list = array();
							$ip_list_count = 0;
							foreach ($_ip_list as $_ip) {
								$ip = substr($_ip, 0, strrpos($_ip, ".")) . ".*";
								$ip_list[$ip] = $ip;
								$ip_list_count++;
								if ($ip_list_count > 10)
									break;
							}
						} else {
							$ip_list = $_ip_list;
						}
						$robot_ip_list[$_robot] = implode('<br />', $ip_list);
					}
				}
			}
			//pre($robots);pre($robot_ip_list);pre($robot_list);exit;

			shownav('工具', '搜索引擎访问记录');
		showsubmenu('搜索引擎访问记录');
			showformheader('spider');
			showtableheader();
			showsubtitle(
					array(
						'<input class="checkbox" type="checkbox" name="portalall" onclick="checkAll(\'prefix\', this.form, \'delete\', \'portalall\')">',
						'名称',
						'今日来访次数',
						'总次数',
						'最后访问时间',
						'IP',
						'记录'
					)
			);
			foreach ((array) $robot_list as $robot) {
				$type = sprintf('%04b', $friendlink['type']);
				showtablerow('', array('class="td25"', 'class="td28"', '', '', 'class="td26"'), array(
					'<input type="checkbox" class="checkbox" name="delete[]" value="' . $robot['name'] . '" />',
				$robot['link'] ? '<a href="' . $robot['link'] . '" target="_blank" title="' . $robot['agent'] . '">' . $robot['name'] . '</a>' : '<span title="' . $robot[agent] . '">' . $robot[name] . '</span>',
					$robot[today_times],
					$robot[times],
					$robot[last_visit],
					'<a href="index.php?group=admin&option=spider&task=viewip&robot=' . $robot[name] . '" title="显示详细信息">' . $robot_ip_list[$robot[name]] . '</a>',
				'
					<a href="index.php?group=admin&option=spider&task=view&name=' . $robot[name] . '&day=5">5 天</a>
					<a href="index.php?group=admin&option=spider&task=view&name=' . $robot[name] . '&day=15">15 天</a>
					<a href="index.php?group=admin&option=spider&task=view&name=' . $robot[name] . '&day=30">30 天</a>
					'
				));
			}

			showsubmit('submit', '提交', 'del');
			showtablefooter();
			showformfooter();
	}
	function View() {
		$name = trim($_GET['name']);
		$day = (int) ($_GET['day']);
		if ($name == "")
			$this->error("名称不能为空", "admin.php?m=spider&a=ls");
		if ($day < 1)
			$this->error("时间周期不能小于1", "admin.php?m=spider&a=ls");
		$date_from = date("Ymd", time() - ($day * 86400));
		$date_to = date("Ymd");
		$logmodel = D('SpiderLog');
		$lists = $logmodel->where("`name` ='$name' and (`date`>{$date_from} and `date`<={$date_to})")->select();
		$times = 0;
		foreach ((array) $lists as $row) {
			$row['first_visit'] = dgmdate($row['first_visit'], "H:i:s", 0);
			$row['last_visit'] = dgmdate($row['last_visit'], "H:i:s", 0);
			$times+=$row['times'];
			$log_list[] = $row;
		}
		$count = sizeof($log_list);
		$nav = "以下是 <b>$name</b> 近 $day 天的来访记录,共 $count 条。";
		shownav('工具', $nav);
		showsubmenu($nav, array(
			array('列表', 'spider', 0)
		));
		showtableheader();
		showsubtitle(
				array(
					'日期',
					'次数',
					'首次来访时间',
					'最后来访时间'
				)
		);
		foreach ((array) $log_list as $robot) {
			$type = sprintf('%04b', $friendlink['type']);
			showtablerow('', '', array(
				$robot['date'],
				$robot['times'],
				$robot['first_visit'],
				$robot['last_visit']
			));
		}
		showtablefooter();
	}

}

?>